define(function (require, exports, module) {
	//基类
	var UIComponent = require("uicomponent-2.0.0");
	var HandlerBars = require("handlebars");
	//工具
	var tools = require("tools");
	var template = require("../assets/tpl/roompanorama_dialog.tpl");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	
	var Text_1_0_0 = UIComponent.extend({
		attrs : {
			template : template,
			model : {
				id : null,
				data:[]
			},
			widget:null
		},
		events : {
			"click .roompanorama-close":function(e){
				this.remove(e);	
			},
			"click #roomYes":function(e){
				this.addListClick(e);
			},
			"click #roomIdNumber":function(e){
				this.getIDCardCode();
			}
		},
		addListClick:function(e){
			var that = this;
			var model = this.get("model");
			if(!$("#roomIdNumber").val()){
				$(".roompanorama-tip",this.element).show();
				$(".roompanorama-tip",this.element).text("请输入身份证号");
				return;
			}
			if(model.data.length >= 4){
				$(".roompanorama-tip",this.element).show();
				$(".roompanorama-tip",this.element).text("该房间最多入住4人");
			}
			if(model.isVerify){
				var personInfoList = model.personInfoList;
				var idNumber = $("#roomIdNumber").val();
				var list = $.grep(personInfoList,function(n,i){
					if(idNumber == n.idNumber){
						return n;
					}
				});
				if(list.length>0){
					that.addData(list[0]);
					$(".roompanorama-tip",this.element).hide();
				}else{
					$(".roompanorama-tip",this.element).show();
					$(".roompanorama-tip",this.element).text("该人员未预约");
				}
				$("#roomIdNumber").val("");
				$("#roomName").val("");
        		$("#roomSex").val("");
			}else{
				var tsex = {};
				if($("#roomSex").val() == "男"){
					tsex={
						key:"Male",
						value:"男"
					}
				}else if($("#roomSex").val() == "女"){
					tsex={
							key:"Female",
							value:"女"
						}
				}
				
				that.getMemberInfo(function(data){
					if(data.length>0){
						data[0].personalInfo.member = true;
						that.addData(data[0].personalInfo);
					}else{
						if(!$("#roomIdNumber").val()){
							$(".roompanorama-tip",this.element).show();
							$(".roompanorama-tip",this.element).text("请输入身份证号");
							return;
						}
						
						if(!$("#roomName").val()){
							$(".roompanorama-tip",this.element).show();
							$(".roompanorama-tip",this.element).text("请输入姓名号");
							return;
						}
						
						if(!$("#roomSex").val()){
							$(".roompanorama-tip",this.element).show();
							$(".roompanorama-tip",this.element).text("请输入性别");
							return;
						}
						
						that.addData({
							idNumber: $("#roomIdNumber").val(),
							name: $("#roomName").val(),
							sex: tsex,
							member:false
						});
					}
					$("#roomIdNumber").val("");
					$("#roomName").val("");
	        		$("#roomSex").val("");
				});
				
			}
		},
		getIDCardCode : function() {
			$.ajax({
				url : "https://localhost:9443/server/IDCardCode?="+new Date().getTime(),
				timeout : 30*1000,
				dataType : "json",
				success : function(peolpe) {
					$("#roomIdNumber").val(peolpe.idNo);
					$("#roomName").val(peolpe.name);
	        		$("#roomSex").val(peolpe.sex);
				}
			});
		},
		getMemberInfo:function(callback){
			var that = this;
			var model = this.get("model");
			var idNumber = $("#roomIdNumber").val();
			if(!idNumber){
				$(".roompanorama-tip",this.element).show();
				$(".roompanorama-tip",this.element).text("请输入身份证号");
				return;
			}
			$(".roompanorama-tip",this.element).hide();
			$(".roompanorama-tip",this.element).text("");
			
			if(model.data.length >= 4){
				$(".roompanorama-tip",this.element).show();
				$(".roompanorama-tip",this.element).text("该房间最多入住4人");
			}else{
				aw.ajax({
	                url: "api/member/query",
	                data: {
	                	"personalInfo.idNumber":idNumber,
	                	"fetchProperties":"pkMember,version,personalInfo.*"
	                },
	                success: callback
	            });
			}
		},
		afterRender : function(){
			var that = this;
			var model = this.get("model");
			this.refresh();
		},
		liTemplate:'<li><span>{{this.name}}</span><span style="margin: auto 20px;">{{this.sex.value}}</span><span>{{this.idNumber}}</span><div class="roompanorama-close" data-index="{{this.index}}"></div></li>',
		setData : function(iData){
			var model = this.get("model");
			model.data = iData;
			$(".roompanorama-list ul",this.element).html("");
			for(var i=0;i<model.data.length;i++){
				model.data[i].index = i;
				var html = HandlerBars.compile(this.liTemplate)(model.data[i]);
				$(".roompanorama-list ul",this.element).append(html);
			}
		},
		getData : function(){
			return this.get("model").data;
		},
		remove:function(e){
			var index = $(e.currentTarget).attr("data-index");
			var model = this.get("model");
			model.data.splice(index, 1);
			this.refresh();
		},
		addData:function(obj){
			var model = this.get("model");
			if(model.data.length < 4){
				model.data.push(obj);
				this.refresh();
			}
		},
		refresh:function(){
			var model = this.get("model");
			this.setData(model.data);
		},
		reset:function(){
			this.setData([]);
		},
	});
	
	module.exports = Text_1_0_0;
	
});